
<?php
import("@.Action.CommonAction");
class AjaxAction extends Common {
	public function AjaxProductSubType(){
		
		$Code = $_POST['Code'];
		$ProductSubTypeClass = $_POST['Class'];
		
		if($Code != "0"){
			$Model = new Model();

			$SourceTable = "";
			$SourceField = "";
			switch ($ProductSubTypeClass)
			{
			case "SecondProductSubTypeID":
				$SourceTable = "SecondProductSubTypes";
				$SourceField = "SecondProductSubType";
				break;  
			case "ThirdProductSubTypeID":
				$SourceTable = "ThirdProductSubTypes";
				$SourceField = "ThirdProductSubType";
				break;  
			case "FourthProductSubTypeID":
				$SourceTable = "FourthProductSubTypes";
				$SourceField = "FourthProductSubType";
				break;  
			case "FifthProductSubTypeID":
				$SourceTable = "FifthProductSubTypes";
				$SourceField = "FifthProductSubType";
				break;  
			case "SixthProductSubTypeID":
				$SourceTable = "SixthProductSubTypes";
				$SourceField = "SixthProductSubType";
				break;  
			case "SeventhProductSubTypeID":
				$SourceTable = "SeventhProductSubTypes";
				$SourceField = "SeventhProductSubType";
				break;  
			default:
				$SourceTable= "";
				break;  
			}

			
			if($SourceTable != "" )
			{
				$sqlB = new StringBuilder();
				$sqlB->Append("SELECT ");
				$sqlB->Append("   Code ");
				$sqlB->Append(" , " . $SourceField . " AS ProductSubType ");
				$sqlB->Append("FROM  " . $SourceTable . " ");
				$sqlB->Append("WHERE " . $SourceTable . ".ParentCode = " . $Code . " ");
				$sqlB->Append(";");

				$ProductSubType = $Model->query($sqlB->ToString());
				$json_string = json_encode($ProductSubType);
				echo $json_string;
			}
			else{
				echo "[]";
			}
		}
		else{
			echo "[]";
		}
	}

	public function DeleteImage(){
	
		$ImageURL = $_POST['ImageURL'];
		$TemplateType = $_POST['TemplateType'];
		$ImportSheetTableID = $_POST['ImportSheetTableID'];


		$TableArray = $this->GetTemplateTypeTable($TemplateType);
		$ImportSheetTable = $TableArray["ImportSheetTable"];
		$ImportSheetTransTable = $TableArray["ImportSheetTransTable"];

		$ColorName = $_POST['ColorName'];
		$Model = new Model();
		if($ImageURL == "main_image_url"){
			$sqlB = new StringBuilder();
			$sqlB->Append("UPDATE " . $ImportSheetTable . " " );
			$sqlB->Append("SET ");
			$sqlB->Append("   main_image_url = Null ");
			$sqlB->Append(" , swatch_image_url = Null ");
			$sqlB->Append("WHERE " . $ImportSheetTable . " .ID = ".$ImportSheetTableID . " ");
			$sqlB->Append(";");

			$Model->execute($sqlB->ToString());

			$sqlB = new StringBuilder();
			$sqlB->Append("UPDATE " . $ImportSheetTransTable . " ");
			$sqlB->Append("SET ");
			$sqlB->Append("   main_image_url = Null");
			$sqlB->Append(" , swatch_image_url = Null ");
			$sqlB->Append("WHERE  " . $ImportSheetTransTable . ".ParentID = " . $ImportSheetTableID . " ");
			$sqlB->Append("AND  " . $ImportSheetTransTable . ".color_name = " . FData($ColorName, "Text") . " ");
			$sqlB->Append(";");
			$Model->execute($sqlB->ToString());
		}
		else{
			$sqlB = new StringBuilder();
			$sqlB->Append("UPDATE " . $ImportSheetTable . " ");
			$sqlB->Append("SET ");
			$sqlB->Append("   " . $ImageURL . " = Null ");
			$sqlB->Append("WHERE " . $ImportSheetTable . ".ID = ".$ImportSheetTableID . " ");
			$sqlB->Append("AND " . $ImportSheetTable . ".color_name = ".FData($ColorName, "Text") . " ");
			$sqlB->Append(";");
			$Model->execute($sqlB->ToString());


			$sqlB = new StringBuilder();
			$sqlB->Append("UPDATE  " . $ImportSheetTransTable . " ");
			$sqlB->Append("SET ");
			$sqlB->Append("   " . $ImageURL . " = Null ");
			$sqlB->Append("WHERE  " . $ImportSheetTransTable . ".ParentID = ".$ImportSheetTableID . " ");
			$sqlB->Append("AND  " . $ImportSheetTransTable . ".color_name = ".FData($ColorName, "Text") . " ");
			$sqlB->Append(";");
			$Model->execute($sqlB->ToString());
		}
		
		echo "ok";
	}

	public function DeleteProductSize(){

		$ProductSize = $_POST['ProductSize'];
		$TemplateType = $_POST['TemplateType'];
		$ImportSheetTableID = $_POST['ImportSheetTableID'];
		

		if($ProductSize != "" && $ImportSheetTableID != "" && $TemplateType != ""){
			$Model = new Model();
			$sqlB = new StringBuilder();

			$TableArray = $this->GetTemplateTypeTable($TemplateType);

			$sqlB->Append("DELETE FROM  " . $TableArray['ImportSheetTransTable'] ." " );
			$sqlB->Append("WHERE " . $TableArray['ImportSheetTransTable'] . ".ParentID = ".$ImportSheetTableID . " ");
			$sqlB->Append("AND " . $TableArray['ImportSheetTransTable'] . ".size_name = " . FData($ProductSize, "Text") . " ");
			$sqlB->Append(";");
			$Model->execute($sqlB->ToString());
		
			echo "ok";
		}
		else{
			echo "error";
		}
	}

	public function DeleteProductColor(){

		$ProductColor = $_POST['ProductColor'];
		$TemplateType = $_POST['TemplateType'];
		$ImportSheetTableID = $_POST['ImportSheetTableID'];

		if($ProductColor != "" && $ImportSheetTableID != ""  && $TemplateType != ""){

			
			$TableArray = $this->GetTemplateTypeTable($TemplateType);

			$Model = new Model();
			$sqlB = new StringBuilder();
			$sqlB->Append("DELETE FROM  " . $TableArray['ImportSheetTransTable'] . " ");
			$sqlB->Append("WHERE " . $TableArray['ImportSheetTransTable'] . ".ParentID = " . $ImportSheetTableID . " ");
			$sqlB->Append("AND " . $TableArray['ImportSheetTransTable'] . ".color_name = " . FData($ProductColor, "Text") . " ");
			$sqlB->Append(";");
			
			$Model->execute($sqlB->ToString());
			echo "ok";
		}
		else{
			echo "error";
		}
	}



	public function GetTemplateTypeTable($TemplateType){
	
		$Array = ["ImportSheetTable" => "","ImportSheetTransTable" => ""];
		if($TemplateType != ""){
			if($TemplateType == "Shoes"){
						
				$Array['ImportSheetTable'] = "ImportProductSheet";
				$Array['ImportSheetTransTable'] = "ImportProductTransSheet";
			}
			elseif($TemplateType == "Clothing"){
				$Array['ImportSheetTable'] = "ClothingImportSheet";
				$Array['ImportSheetTransTable'] = "ClothingImportTransSheet";
			}
		}

		return $Array;
	}
}